package com.iniyo.ai.poetry.util;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;

public class HttpUtil {

    public void postWithJson(String url, Object params) {
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        String requestBody = JSONObject.valueToString(params);
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(requestBody, mediaType))
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body());
            }
        });
    }
}
